//
// Created by denglibin on 2020/2/23.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define USERNAME_LEN 32
// 用户信息结构体
typedef struct user
{
    char *username; // 用户名称
    char *password; // 用户密码
    char *name;     // 用户姓名
    // 其他信息...
    struct user *next; // 下一个节点
} User;
/**
 * 打印用户信息
 * @param head 头节点
 */
void show_user(User *head)
{
    User *temp = head;
    if (temp == NULL)
    {
        printf("暂无用户信息,请先注册\n");
        return;
    }
    printf("姓名\t用户名\t密码\n");
    while (temp)
    {
        printf("%s\t%s\t%s\n", temp->name, temp->username, temp->password);
        temp = temp->next;
    }
}

/**
 * 销毁用户信息
 * @param head 头节点
 */
void release(User *head)
{
    User *temp = head;
    if (temp == NULL)
    {
        return;
    }
    User *next = temp->next;
    while (temp)
    {
        printf("%s已销毁\n", temp->username);
        free(temp->name);
        free(temp->username);
        free(temp->password);
        free(temp);
        temp = next;
        if (temp != NULL)
        {
            next = temp->next;
        }
    }
}

/**
 * 保存用户信息
 * @param head 头节点
 * @param user 保存的用户信息
 */
User *insert(User *head, User *user)
{

    if (head == NULL)
    {
        return user;
    }
    head->next = user;
    return head;
}
/**
 * 根据用户名查找
 * @param head 头节点
 * @param username 用户名
 */
User *query1(User *head, const char *username)
{
    User *temp = head;

    int r = 0;
    while (temp)
    {
        r = strcmp(temp->username, username);
        if (r == 0)
        {
            return temp;
        }
        temp = temp->next;
    }
    return NULL;
}
/**
 * 根据用户名和密码查找
 * @param head 头节点
 * @param username 用户名
 * @param password 密码
 */
User *query2(User *head, const char *username, const char *password)
{
    User *temp = head;
    int r = 0;
    while (temp)
    {
        r = strcmp(temp->username, username);
        if (r == 0)
        {
            r = strcmp(temp->password, password);
            if (r == 0)
            {
                return temp;
            }
            return NULL;
        }
        temp = temp->next;
    }
    return NULL;
}

/**
 * 注册
 * @param head 头节点(没有传NULL)
 * @return 头节点
 */
User *sign(User *head)
{
    char *username = NULL;
    char *password = NULL;
    char *name = NULL;
    printf("请输入用户名(英文和数字，最长%d位):\n", USERNAME_LEN);
    username = (char *)calloc(USERNAME_LEN, sizeof(char));

    scanf("%s", username);

    User *user = query1(head, username);
    int flag = (user != NULL);
    while (flag)
    {
        printf("用户名已存在，请重新输入:\n");
        scanf("%s", username);
        flag = (strcmp(user->username, username) == 0);
    }
    printf("username:%s\n", username);

    printf("请输入密码(最长%d位):\n", USERNAME_LEN);
    password = (char *)calloc(USERNAME_LEN, sizeof(char));
    scanf("%s", password);
    printf("password:%s\n", password);

    printf("请输入姓名(最长%d位):\n", USERNAME_LEN);
    name = (char *)calloc(USERNAME_LEN, sizeof(char));
    scanf("%s", name);
    printf("name:%s\n", name);

    User *new_user = (User *)calloc(1, sizeof(User));
    new_user->username = username;
    new_user->password = password;
    new_user->name = name;
    new_user->next = NULL;
    head = insert(head, new_user);
    printf("注册完成\n");
    return head;
}

/**
 * 登录
 * @param head 头节点
 * @return 登录成功后的用户信息 登录失败返回NULL
 */
User *login(User *head)
{
    char *username = NULL;
    char *password = NULL;
    int flag = 1;
    User *current_user = NULL;
    while (current_user == NULL)
    {

        printf("请输入用户名:\n");
        username = (char *)calloc(USERNAME_LEN, sizeof(char));
        scanf("%s", username);
        printf("请输入密码:\n");
        password = (char *)calloc(USERNAME_LEN, sizeof(char));
        scanf("%s", password);

        current_user = query2(head, username, password);
        if (current_user == NULL)
        {
            printf("用户名或密码错误,输入1继续登录,输入0取消登录：");
            scanf("%d", &flag);
            if (flag == 0)
            {
                return NULL;
            }
        }
    }

    printf("登录成功,欢迎您:%s\n", current_user->name);
    return current_user;
}

int main25(int argc, char *argv[])
{
    User *head = NULL;
    int num = 1;
    printf("\n");
    printf("***************欢迎进入***************\n");
    printf("0：退出系统   \t1：注册新用户\n");
    printf("\n");
    printf("2：展示所有用户\t3：清除所有用户\n");
    printf("4：登录\t\n");
    printf("\n");
    printf("**************************************\n");
    printf("\n");

    while (num)
    {
        printf("输入编号，执行操作:");
        scanf("%d", &num);
        switch (num)
        {
        case 0:
            release(head);
            printf("再见,欢迎下次登陆\n");
            break;
        case 1:
            head = sign(head);
            break;
        case 2:
            show_user(head);
            break;
        case 3:
            release(head);
            head = NULL;
            break;
        case 4:
            login(head);
            break;
        default:
            printf("不支持的操作\n");
            break;
        }
    }
    return 0;
}
